package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.AuthAccess;
import com.example.springboot.entity.GoodsPlus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.example.springboot.service.IGoodsPlusService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wgc
 * @since 2023-09-13
 */
@RestController
@RequestMapping("/goodsPlus")
public class GoodsPlusController {

    @Resource
    private IGoodsPlusService goodsPlusService;

    //新增和修改
    @PostMapping
    public Result save(@RequestBody GoodsPlus goodsPlus) {  //@RequestBody接收前端数据
        if (goodsPlus.getId() == null) {
            goodsPlus.setTime(DateUtil.now());
//            goodsPlus.setUser(TokenUtils.getCurrentUser().getNickname());
        }
        return Result.success(goodsPlusService.saveOrUpdate(goodsPlus));
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(goodsPlusService.list());
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(goodsPlusService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(goodsPlusService.removeByIds(ids));
    }

    //    用来逃避拦截器 token
    @AuthAccess
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(goodsPlusService.getById(id));
    }

//    用来逃避拦截器 token
    @AuthAccess
    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {

        QueryWrapper<GoodsPlus> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(goodsPlusService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}



